package com.example.demo.Repository;

import com.example.demo.Entity.Collect;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface CollectRepository extends JpaRepository<Collect, Long> {

    // 根据用户ID查找所有收藏记录
    List<Collect> findByUserId(Long userId);

    // 根据项目ID和用户ID删除收藏记录
    void deleteByItemIdAndUserId(Long itemId, Long userId);

    // 检查某个项目是否已被特定用户收藏
    @Query("SELECT COUNT(c) > 0 FROM Collect c WHERE c.item.id = :itemId AND c.user.id = :userId")
    boolean existsByItemIdAndUserId(@Param("itemId") Long itemId, @Param("userId") Long userId);
}